package com.sixth.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import com.sixth.core.entity.CareOrderItem;
import com.sixth.core.entity.OrderCharge;
import com.sixth.core.entity.OrderChargeItem;
import com.sixth.core.vo.R;
import com.sixth.mapper.CareOrderItemMapper;
import com.sixth.mapper.OrderChargeItemMapper;
import com.sixth.mapper.OrderChargeMapper;
import com.sixth.service.OrderChargeService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class MyListener {

    @Resource
    private OrderChargeMapper orderChargeMapper;

    @Resource
    private OrderChargeService orderChargeService;

    @Resource
    private OrderChargeItemMapper orderChargeItemMapper;

    @Resource
    private CareOrderItemMapper careOrderItemMapper;

    @RabbitListener(queues = "wx_order_queue")
    public void fund(Message message, Channel channel) {
        try {
            // 调用 queryPayStatus，获取支付状态
            R result = orderChargeService.queryPayStatus2(new String(message.getBody()));
            // 使用 getCode() 判断返回状态
            if (result.getCode() == 200) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);  // 确认消息
            } else {
                Thread.sleep(1000);
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, true);  // 消息拒绝，并重新入队
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @RabbitListener(queues = "wx_dlx_queue")
    public void fund2(Message message, Channel channel) {
        String chId = new String(message.getBody());

        // 通过 chId 查询 OrderCharge 对象
        OrderCharge orderCharge = orderChargeMapper.selectById(chId);

        if (orderCharge != null) {
            // 确认订单状态并更新
            if ("0".equals(orderCharge.getOrderStatus())) {
                orderCharge.setOrderStatus("2");  // 更新订单状态为已超时
                orderChargeMapper.updateById(orderCharge);

                // 查询 OrderChargeItem 列表
                String orderId = orderCharge.getOrderId();
                QueryWrapper<OrderChargeItem> queryWrapperItem = new QueryWrapper<>();
                queryWrapperItem.eq("order_id", orderId);
                List<OrderChargeItem> orderChargeItems = orderChargeItemMapper.selectList(queryWrapperItem);

                // 获取 coIds 列表
                List<String> itemIds =  orderChargeItems.stream()
                        .map(OrderChargeItem::getItemId)
                        .collect(Collectors.toList());

                // 查询 CareOrderItem 列表
                QueryWrapper<CareOrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.in("item_id", itemIds);
                List<CareOrderItem> careOrderItems = careOrderItemMapper.selectList(queryWrapper);

                // 更新每条 CareOrderItem 的状态
                for (CareOrderItem careOrderItem : careOrderItems) {
                    careOrderItem.setStatus("5");
                    careOrderItemMapper.updateById(careOrderItem);
                }

            }
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);  // 确认消息
            } catch (IOException e) {
                // 捕获并处理 IO 异常
                throw new RuntimeException("Failed to ack message", e);
            }

        }

    }
}
